package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.utils.StringUtils;
import com.jservice.model.SmkAppUser;
import com.sun.xml.internal.rngom.parse.host.Base;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Upp2031 - 转市民卡号
 *
 * @Author ycj
 * @DATE 2017/6/14.
 */
@Service("upp2031")
public class Upp2031 extends BaseService implements IService {

    private String SERVER_NAME = "upp.card.si2citizen.query";


    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws Exception {
        Map paramMap = request.getData();
        String input_no = paramsStringFilter(paramMap.get("input_no")); // 用户输入数据

        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null) {
            return renderFAIL(MsgCode.Code_1007, response, header);
        }
        Map data = new HashMap();
        if (StringUtils.isNotEmpty(input_no)) {
            if (input_no.length() == 9) {  //若长度为9位,填si_no。
                data.put("si_no", input_no);
            } else {
                data.put("id_no", input_no);
            }
        } else {
            return renderFAIL(MsgCode.Code_1001, response, header);
        }
        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME, request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            return renderSUC(jsonObject, response, header);
        } else {
            String msg = jsonObject.getString("msg");
            String errMsg = "";
            if (msg.equals("[温州市民卡运营平台]提醒您:市民卡卡号验证失败")){
                errMsg = "输入有误,请核对";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:商户或网点未授权操作当前卡类型")){
                errMsg = "该市民卡状态异常，无法进行充值！";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:参数值不正确")){
                errMsg = "输入有误,请核对";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:短信验证码错误")){
                errMsg = "短信验证码错误";
            }else
            if (msg.equals("市民卡网关返回异常:服务密码不等于6位")){
                errMsg = "服务密码不正确";
            }else{
                errMsg = msg;
            }
            return renderFAIL(MsgCode.Code_1009,errMsg,response,header);
        }
    }
}
